package Simulation;

import java.util.Vector;

import fxana.Range;

import fxana.FxRawDataSet;
import fxana.RawDataCell;

public class ConditionRLSM extends ConditionR {
	/* This construct is for seed */
	public ConditionRLSM(){
		super();
	}
	public ConditionRLSM(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		super(ds, today, flag);
	}

	@Override
	protected void PreProcess(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
	
		if(flag.IsLFlagSet()&&flag.GetLFlagIterator().hasNext()){
			super.LossCutValue=flag.GetLFlagIterator().next()/10.0;
		}
	}

	@Override
	protected Condition GetNew(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		return new ConditionRLSM(ds,today,flag);
	}

	@Override
	protected RawDataCell GetEstimatedValue(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		Integer numberofday=25;
		
		
		Vector<Range>tmp=flag.GetRangeSet();
		if(!tmp.isEmpty()){
			int i=0;
			for(i=0;i<tmp.size();i++){
				if(tmp.elementAt(i).GetType()==Range.TYPE_DATE){
					if(!tmp.elementAt(i).InRange(today.GetRecDate())){
						return null;
					}
				}
			}
		}
		
		if(flag.IsMFlagSet()&&flag.GetMFlagIterator().hasNext()){
			numberofday=flag.GetMFlagIterator().next();
		}
		
		RawDataCell est=null;
		numberofday--;
		do{
			numberofday++;
			if(ds.GetIndexByDate(today.GetRecDate())-numberofday<=0){
				break;
			}
			est=ds.GetLSMDataByDate(today.GetRecDate(), numberofday);
		}while(est==null);
		
		return est;
	}
	@Override
	protected void UpdateLossCutValue(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		return;
	}
}
